package com.iring.controller.admin;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iring.controller.BaseController;
import com.iring.domain.common.DeviceInfo;
import com.iring.domain.common.view.DeviceInfoView;
import com.iring.domain.common.view.ServiceView;
import com.iring.vo.common.BooleanResultVO;
import com.iring.vo.common.ResultVO;
import com.iring.vo.common.device.CheckDomainVO;
import com.iring.vo.common.device.DeviceDdnsVO;
import com.iring.vo.common.device.DeviceUsernameVO;
import com.iring.vo.common.device.MacVO;
import com.iring.vo.common.device.ModDeviceVO;
import com.iring.vo.sms.SMSSendVO;

/**
 * 设备信息Controller
 *
 * @author wanggan
 */
@RestController
@RequestMapping(value = "/admin/device", produces = { "application/json;charset=UTF-8" })
public class DeviceController extends BaseController {

    /**
     * 获取用户设备信息列表
     *
     * @return 用户设备列表
     */
    @RequestMapping(value = "/list-device", method = RequestMethod.POST)
    public List<DeviceInfoView> listDevice() {
        return deviceService.listDevice(getUserSession().getUsername());
    }

    /**
     * 获取单个设备信息
     *
     * @param did
     *            设备id
     * @return 返回设备信息
     */
    @RequestMapping(value = "/get-device-info", method = RequestMethod.GET)
    public DeviceInfoView getDeviceInfo(@RequestParam("id") Integer did) {
        return deviceService.getDeviceInfo(did, getUserSession().getUsername());
    }

    /**
     * 获取当前话机所开的套餐
     *
     * @param macVO
     *            查询已经开启服务的参数
     * @return 话机所开服务列表
     */
    @RequestMapping(value = "/list-phone-service", method = RequestMethod.POST)
    public List<ServiceView> listPhoneService(@RequestBody MacVO macVO) {
        return selectService.listPhoneService(macVO, getUserSession().getUsername());
    }

    /**
     * 检查话机号码
     *
     * @param SMSSendVO
     *            {@link SMSSendVO}
     * @return 话机是否被使用
     */
    @RequestMapping(value = "/is-iringtel-num-available", method = RequestMethod.POST)
    public ResultVO isIringtelNumAvailable(@RequestBody SMSSendVO SMSSendVO) {
        return new ResultVO(deviceService.isIringtelNumAvailable(SMSSendVO));
    }

    /**
     * 发送验证码
     *
     * @param SMSSendVO
     *            {@link SMSSendVO}
     * @return 发送结果
     */
    @RequestMapping(value = "/send-valid-code", method = RequestMethod.POST)
    public ResultVO sendValidCode(@RequestBody SMSSendVO SMSSendVO) {
        checkSessionPhoneToken();
        Boolean result = deviceService.sendValidCode(SMSSendVO);
        updateSessionPhoneTimeStamp();
        return new ResultVO(result);
    }

    /**
     * 更新话机号码
     *
     * @param modDeviceVO
     *            {@link ModDeviceVO}
     * @return 更新结果
     */
    @RequestMapping(value = "/update-device-iringtel-num", method = RequestMethod.POST)
    public BooleanResultVO updateDeviceIringtelNum(@RequestBody ModDeviceVO modDeviceVO) {
        deviceService.updateDeviceIringtelNum(modDeviceVO);
        removeSessionPhoneTimeStamp();
        String username = getUserSession().getUsername();
        logService.saveLog(username,"update-device-iringtel-num",getUserRealIp(),true);
        return new BooleanResultVO(Boolean.TRUE);
    }

    /**
     * 修改设备的用户名
     *
     * @param deviceUsernameVO
     *            {@link DeviceDdnsVO}
     * @return 修改结果
     */
    @RequestMapping(value = "/update-device-username", method = RequestMethod.POST)
    public BooleanResultVO updateDeviceUsername(@RequestBody DeviceUsernameVO deviceUsernameVO) {
        return new BooleanResultVO(deviceService.updateDeviceUsername(deviceUsernameVO,
                getUserSession().getUsername()));
    }

    @RequestMapping(value = "/is-ddns-avaliable", method = RequestMethod.POST)
    public ResultVO isDdnsAvaliablle(@RequestBody MacVO macVO) {
        String tel = deviceService.isDdnsAvaliablle(getUserSession().getUsername(),macVO);
        return new ResultVO(tel);
    }

    @RequestMapping(value = "/check-ddns-avaliable", method = RequestMethod.POST)
    public ResultVO checkDdnsAvaliablle(@RequestBody CheckDomainVO checkDomainVO) {
        return new ResultVO(deviceService.checkDdnsAvaliablle(getUserSession().getUsername(),checkDomainVO));
    }


    /**
     * 发送修改ddns的验证码
     *
     * @return 发送验证码结果
     */
    @RequestMapping(value = "/send-ddns-verify-code", method = RequestMethod.POST)
    public BooleanResultVO sendDdnsVerifyCode() {
        checkSessionPhoneToken();
        Boolean issend = deviceService.sendDdnsVerifyCode(getUserSession().getUsername());
        updateSessionPhoneTimeStamp();
        return new BooleanResultVO(issend);
    }

    /**
     * 修改设备域名
     *
     * @param deviceDdnsVO
     *            修改设备ddns的参数
     * @return 更新成功true和失败false
     */
    @RequestMapping(value = "/update-device-ddns", method = RequestMethod.POST)
    public BooleanResultVO updateDeviceDdns(@RequestBody DeviceDdnsVO deviceDdnsVO) {
        Boolean issuccess = deviceService.updateDeviceDdns(deviceDdnsVO, getUserSession().getUsername());
        removeSessionPhoneTimeStamp();
        String username = getUserSession().getUsername();
        logService.saveLog(username,"update-device-ddns",getUserRealIp(),true);
        return new BooleanResultVO(issuccess);
    }

    /**
     * 获取vpn连接远程控制
     *
     * @param macVO
     *            {@link MacVO}
     * @return 远程连接的结果
     */
    @RequestMapping(value = "/open-remote-link", method = RequestMethod.POST)
    public ResultVO openRemoteLink(@RequestBody MacVO macVO) {
        String username = getUserSession().getUsername();
        DeviceInfo deviceInfo = deviceService.findDeviceState(macVO,username);
        String result = deviceService.openRemoteLink(deviceInfo);
        logService.saveLog(username,"open-remote-link",getUserRealIp(),true);
        return new ResultVO(result);
    }

    /**
     * 关闭vpn连接
     *
     * @param macVO
     *            {@link MacVO}
     * @return 关闭远程连接结果
     */
    @RequestMapping(value = "/close-remote-link", method = RequestMethod.POST)
    public ResultVO closeRemoteLink(@RequestBody MacVO macVO) {
        String username = getUserSession().getUsername();

        DeviceInfo deviceInfo = deviceService.findDeviceState(macVO,username);
        String result = deviceService.closeRemoteLink(deviceInfo);
        logService.saveLog(username,"close-remote-link",getUserRealIp(),true);
        return new ResultVO(result);
    }

    /**
     * 检查与客户端网络是否畅通
     *
     * @param macVO
     *            {@link MacVO}
     * @return 检查网络的状况
     */
    @RequestMapping(value = "/check-network", method = RequestMethod.POST)
    public ResultVO checkNetwork(@RequestBody MacVO macVO) {
        DeviceInfo deviceInfo = deviceService.findDeviceState(macVO,getUserSession().getUsername());
        return new ResultVO(deviceService.checkNetwork(deviceInfo));
    }
}
